package se.emilsjolander.flipview.items;

import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import se.emilsjolander.library.FoldableItem;

import java.util.ArrayList;
import java.util.List;

public class Items {
  public static String[] titles = new String[]{
      "0:0",
      "1:1",
      "2:2",
      "3:3",
      "4:4",
      "5:5",
      "6:6",
      "7:7",
      "8:8",
      "9:9"
  };

  public static List<FoldableItem> getFoldableItems(Context context) {
    List<FoldableItem> foldableItems = new ArrayList<>();
    Paint paint = new Paint();
    Paint elevationPaint = new Paint();
    paint.setTextSize(dp2px(60, context));
    Paint paint1 = new Paint();
    paint1.setColor(Color.RED);
    Paint textBackground = new Paint();
    Paint text = new Paint();
    text.setTextSize(dp2px(20, context));
    textBackground.setColor(new Color(Color.argb(88, 255, 255, 255)));
    for (int i = 0; i < titles.length; i++) {

      int index = i;
      FoldableItem foldableItem = new FoldableItem() {
        @Override
        public String onDraw(Canvas canvas, int width, int height, float touchX, float touchY) {
          String clickName = "";
          int padding = dp2px(4, context);
          int screenWidth = width - padding * 2;
          int screenHeight = height - padding * 2;
          canvas.drawRect(new RectFloat(0, 0, width, height), paint1);

          int shadowHeight = dp2px(40, context);
          LinearShader gradient =
              new LinearShader(
                  new Point[]{new Point(0, height - padding - shadowHeight), new Point(0,
                      height - padding)},
                  new float[]{0f, 0.3f, 1f},
                  new Color[]{new Color(0x00000000), new Color(0x33000000), new Color(0x88000000)},
                  Shader.TileMode.CLAMP_TILEMODE);

          elevationPaint.setShader(gradient, Paint.ShaderType.LINEAR_SHADER);
          canvas.drawRect(new RectFloat(padding, screenHeight - padding - shadowHeight,
              width - padding, height - padding), elevationPaint);
          canvas.drawText(paint, titles[index], screenWidth / 2 - getTextWidth(paint,
              titles[index]) / 2, screenHeight / 2 + dp2px(25, context));
          canvas.drawRoundRect(new RectFloat(screenWidth / 2 - dp2px(30, context), dp2px(30,
              context), screenWidth / 2 + dp2px(30, context), dp2px(70, context)), 5, 5,
              textBackground);
          canvas.drawText(text, "First", screenWidth / 2 - dp2px(20, context), dp2px(57, context));

          canvas.drawRoundRect(new RectFloat(screenWidth / 2 - dp2px(30, context),
              screenHeight - dp2px(70, context), screenWidth / 2 + dp2px(30, context),
              screenHeight - dp2px(30, context)), 5, 5, textBackground);
          canvas.drawText(text, "Last", screenWidth / 2 - dp2px(20, context),
              screenHeight - dp2px(45, context));
          if (touchX > screenWidth / 2 - dp2px(30, context) &&
              touchX < screenWidth / 2 + dp2px(30, context) &&
              touchY > dp2px(30, context) &&
              touchY < dp2px(70, context)) {
            clickName = "first";
          } else if (touchX > screenWidth / 2 - dp2px(30, context) &&
              touchX < screenWidth / 2 + dp2px(30, context) &&
              touchY > screenHeight - dp2px(70, context) &&
              touchY < screenHeight - dp2px(30, context)) {
            clickName = "last";
          }
          return clickName;
        }
      };
      foldableItems.add(foldableItem);
    }
    return foldableItems;
  }

  public static int dp2px(float dp, Context context) {
    return (int) (context.getResourceManager().getDeviceCapability().screenDensity / 160 * dp + 0.5f);
  }

  public static int getTextWidth(Paint paint, String str) {
    int iRet = 0;
    if (str != null && str.length() > 0) {
      int len = str.length();
      float[] widths = new float[len];
      paint.getAdvanceWidths(str, widths);
      for (int j = 0; j < len; j++) {
        iRet += (int) Math.ceil(widths[j]);
      }
    }
    return iRet;
  }

  public static RectFloat getDrawRectFloat(PixelMap pixelMap, int width, int height) {
    int pixelMapWidth = pixelMap.getImageInfo().size.width;
    int pixelMapHeight = pixelMap.getImageInfo().size.height;
    return getDrawRectFloat(pixelMapWidth, pixelMapHeight, width, height);
  }

  public static RectFloat getDrawRectFloat(int pixelMapWidth, int pixelMapHeight, int width,
                                           int height) {
    if (pixelMapWidth * height > pixelMapHeight * width) {
      return new RectFloat((pixelMapWidth - width * pixelMapHeight / height) / 2, 0,
          (pixelMapWidth + width * pixelMapHeight / height) / 2, pixelMapHeight);
    } else {
      return new RectFloat(0, (pixelMapHeight - height * pixelMapWidth / width) / 2,
          pixelMapWidth, (pixelMapHeight + height * pixelMapWidth / width) / 2);
    }
  }
}
